//============================================================================
// Copyright (C) 2010 Brett R. Jones
// All Rights Reserved
//
// You may redistribute and/or modify for non commercial and commercial uses
// provided this copyright notice remains in place and is not modified
//
// This code is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//
// brettjones1900@gmail.com
// http://www.p2panarchy.com
//============================================================================

#include "DtwGame.h"


//------------------------------------------------------------------------------
//! play button click sound
void DtwStateMenu::PlayButtonClick( void )
{ 
	//log_msg( 0, "Sound buffer %d\n", m_apoMenuSounds[ eMenuSoundClick ] );
	m_apoMenuSounds[ eMenuSoundClick ]->Play();
	//log_msg( 0, "Sound buffer %d done\n", m_apoMenuSounds[ eMenuSoundClick ] );
};

//------------------------------------------------------------------------------
//! Handle events from irrlicht gui controls
bool DtwStateMenu::OnGuiEvent( irr::gui::EGUI_EVENT_TYPE eGuiEventType, irr::gui::IGUIElement * poGuiElement )
{
	switch( m_eCurMenuState )
	{
	case eMenuStateMain:
		return MenuMainGuiEvent( eGuiEventType, poGuiElement );
	case eMenuStateChooseDifficulty:
		return MenuChooseDifficultyGuiEvent( eGuiEventType, poGuiElement );
	case eMenuStateHighScores:
		return MenuHighScoresGuiEvent( eGuiEventType, poGuiElement );
	case eMenuStateOptions:
		return MenuOptionsGuiEvent( eGuiEventType, poGuiElement );
	case eMenuStateVideo:
		return MenuVideoGuiEvent( eGuiEventType, poGuiElement );
	case eMenuStateAudio:
		return MenuAudioGuiEvent( eGuiEventType, poGuiElement );
	case eMenuStateTutorial:
		return MenuTutorialGuiEvent( eGuiEventType, poGuiElement );
	case eMenuStateGameOver:
		return MenuGameOverGuiEvent( eGuiEventType, poGuiElement );
	case eMenuStateGameOverHighScore:
		return MenuGameOverHighScoreGuiEvent( eGuiEventType, poGuiElement );
	case eMenuStateAddHighScore:
		return MenuAddHighScoreGuiEvent( eGuiEventType, poGuiElement );
	case eMenuStateHighScoreLevel:
		return MenuHighScoreLevelGuiEvent( eGuiEventType, poGuiElement );
	default:
		log_msg( 0, "DtwStateMenu::OnGuiEvent: UNKNOWN menu state %d\n", m_eCurMenuState );
		vx_assert( false );
	}
	return false;
}
//------------------------------------------------------------------------------
//! handle gui events for main menu
bool DtwStateMenu::MenuMainGuiEvent( irr::gui::EGUI_EVENT_TYPE eGuiEventType, irr::gui::IGUIElement * poGuiElement )
{
	bool bEaten = false;
	if( irr::gui::EGET_BUTTON_CLICKED != eGuiEventType  ) 
	{
		// handle hover etc here
		return false;
	}
	switch( poGuiElement->getID() ) 
	{
	case EGUI_MAIN_LOGO:
		// go to main website
		break;
	case EGUI_MAIN_PLAY_BUT:
		// show dlg to choose difficulty level game
		m_eCurMenuState = eMenuStateInitChooseDifficulty;
		PlayButtonClick();
		break;
	case EGUI_MAIN_HIGHSCORE_BUT:
		// show high scores
		m_eCurMenuState = eMenuStateInitHighScoreLevel;
		PlayButtonClick();
		break;
	case EGUI_MAIN_OPTIONS_BUT:
		m_eCurMenuState = eMenuStateInitOptions;
		PlayButtonClick();
		break;
	case EGUI_MAIN_TUTORIAL_BUT:
		m_eCurMenuState = eMenuStateInitTutorial;
		PlayButtonClick();
		break;
	case EGUI_MAIN_EXIT_BUT:
		// request that app shutdown and exit
		g_poGameMgr->SetIsShutdownRequested( true );
		break;
	}
	return false;
}
//------------------------------------------------------------------------------
//! handle gui events for choosing game difficulty level
bool DtwStateMenu::MenuChooseDifficultyGuiEvent( irr::gui::EGUI_EVENT_TYPE eGuiEventType, irr::gui::IGUIElement * poGuiElement )
{
	// single player hard/medium/easy
	bool bEaten = false;
	if( irr::gui::EGET_BUTTON_CLICKED != eGuiEventType  ) 
	{
		// handle hover etc here
		return false;
	}
	PlayButtonClick();
	switch( poGuiElement->getID() ) 
	{
	case EGUI_PLAY_EASY_BUT:
		log_msg( 0, "MenuChooseDifficultyGuiEvent: easy\n" );
		//log_msg( 0, "30 Game Instance %d\n", m_poIrrBase->m_iGameInstance  );

		m_iDifficultyLevel = 1;
		m_poGameMgr->m_iGameLevel = 1;
		ChangeSkin( eGuiSkinTypeGame ); // change skin to game mode
		m_eCurMenuState = eMenuStateInitGame;
		g_poGameMgr->m_poNewState = new DtwStatePlay();
		//log_msg( 0, "31 Game Instance %d\n", m_poIrrBase->m_iGameInstance  );
		break;
	case EGUI_PLAY_MEDIUM_BUT:
		log_msg( 0, "MenuChooseDifficultyGuiEvent: medium\n" );
		m_iDifficultyLevel = 2;
		m_poGameMgr->m_iGameLevel = 2;
		ChangeSkin( eGuiSkinTypeGame ); // change skin to game mode
		m_eCurMenuState = eMenuStateInitGame;
		g_poGameMgr->m_poNewState = new DtwStatePlay();
		break;
	case EGUI_PLAY_HARD_BUT:
		log_msg( 0, "MenuChooseDifficultyGuiEvent: hard\n" );
		m_iDifficultyLevel = 3;
		m_poGameMgr->m_iGameLevel = 3;
		ChangeSkin( eGuiSkinTypeGame ); // change skin to game mode
		m_eCurMenuState = eMenuStateInitGame;
		g_poGameMgr->m_poNewState = new DtwStatePlay();
		break;
	case EGUI_PLAY_TUTORIAL:
		m_eCurMenuState = eMenuStateInitTutorial;
		break;

	case EGUI_PLAY_BACK_BUT:
		log_msg( 0, "MenuChooseDifficultyGuiEvent: back\n" );
		// GO BACK TO MAIN MENU
		m_eCurMenuState = eMenuStateInitMain;
		break;
	}
	log_msg( 0, "MenuChooseDifficultyGuiEvent: done\n" );
	return false;
}
//------------------------------------------------------------------------------
//! handle gui events for viewing high scores
bool DtwStateMenu::MenuHighScoresGuiEvent( irr::gui::EGUI_EVENT_TYPE eGuiEventType, irr::gui::IGUIElement * poGuiElement )
{
	// single player hard/medium/easy
	bool bEaten = false;
	if( irr::gui::EGET_BUTTON_CLICKED != eGuiEventType  ) 
	{
		// handle hover etc here
		return false;
	}
	switch( poGuiElement->getID() ) 
	{
	//case EGUI_PICK_HIGHSCORE_HARD_BUT:
	//	m_iDifficultyLevel = 3
	//	m_eCurMenuState = eMenuStateInitMain;
	//	PlayButtonClick();
	//	break;
	//case EGUI_PICK_HIGHSCORE_MEDIUM_BUT:
	//	m_iDifficultyLevel = 2
	//	m_eCurMenuState = eMenuStateInitMain;
	//	PlayButtonClick();
	//	break;
	//case EGUI_PICK_HIGHSCORE_EASY_BUT:
	//	m_iDifficultyLevel = 1
	//	m_eCurMenuState = eMenuStateInitMain;
	//	PlayButtonClick();
	//	break;
	case EGUI_SHOW_HIGHSCORE_BACK_BUT:
		m_eCurMenuState = eMenuStateInitMain;
		PlayButtonClick();
		break;
	}
	return false;
}
// TODO implement view level info
	/*
	// show users play info dlg for the given level
	EGUI_LEVEL_INFO_DESCRIPTION_TEXT	= 140,
	EGUI_LEVEL_INFO_ATTEMPTS_TEXT	= 141,
	EGUI_LEVEL_INFO_WINS_TEXT		= 142,
	EGUI_LEVEL_INFO_LOSES_TEXT		= 143,
	EGUI_LEVEL_INFO_PLAYTIME_TEXT	= 144,
	EGUI_LEVEL_INFO_BESTTIME_TEXT	= 145,
	EGUI_LEVEL_INFO_BACK_BUT		= 145,
	*/

//------------------------------------------------------------------------------
//! handle gui events for options menu
bool DtwStateMenu::MenuOptionsGuiEvent( irr::gui::EGUI_EVENT_TYPE eGuiEventType, irr::gui::IGUIElement * poGuiElement )
{
	// single player hard/medium/easy
	bool bEaten = false;
	if( irr::gui::EGET_BUTTON_CLICKED != eGuiEventType  ) 
	{
		if( irr::gui::EGET_CHECKBOX_CHANGED == eGuiEventType  ) 
		{
			irr::gui::IGUICheckBox * poShowTut = static_cast<irr::gui::IGUICheckBox *>(m_poGui->getRootGUIElement()->getElementFromId(EGUI_OPTIONS_SHOW_TUTORIAL));
			g_UserConfigImp.m_bShowTutorial = poShowTut->isChecked();
			g_UserConfigImp.WriteConfig();
			PlayButtonClick();
		}
		// handle hover etc here
		return false;
	}
	switch( poGuiElement->getID() ) 
	{
	case EGUI_OPTIONS_VIDEO:
		m_eCurMenuState = eMenuStateInitVideo;
		PlayButtonClick();
		break;
	case EGUI_OPTIONS_AUDIO:
		m_eCurMenuState = eMenuStateInitAudio;
		PlayButtonClick();
		break;
	case EGUI_OPTIONS_SHOW_TUTORIAL:
		{
		}
		break;
	//case EGUI_OPTIONS_CONTROLS:
	//	m_eCurMenuState = eMenuStateInitMain;
	//	PlayButtonClick();
	//	break;
	case EGUI_OPTIONS_BACK:
		m_eCurMenuState = eMenuStateInitMain;
		PlayButtonClick();
		break;
	}
	return false;
}
//------------------------------------------------------------------------------
//! handle gui events for video options
bool DtwStateMenu::MenuVideoGuiEvent( irr::gui::EGUI_EVENT_TYPE eGuiEventType, irr::gui::IGUIElement * poGuiElement )
{
	// single player hard/medium/easy
	bool bEaten = false;
	if( irr::gui::EGET_BUTTON_CLICKED != eGuiEventType  ) 
	{
		// handle hover etc here
		return false;
	}
	switch( poGuiElement->getID() ) 
	{
	case EGUI_VIDEO_VIDEOMODES:
		PlayButtonClick();
		break;
	case EGUI_VIDEO_ANTIALIAS:
		PlayButtonClick();
		break;
	case EGUI_VIDEO_SHADOWS:
		PlayButtonClick();
		break;
	case EGUI_VIDEO_SAVE:
		PlayButtonClick();
		/*
		// Save the video mode
		irr::gui::IGUIComboBox *VideoModes = static_cast<irr::gui::IGUIComboBox *>(m_poGui->getRootGUIElement()->getElementFromId(EGUI_VIDEO_VIDEOMODES));
		if(VideoModes != NULL) 
		{
			VideoModeStruct Mode = Graphics::Instance().GetVideoModes()[VideoModes->getSelected()];
			g_UserConfigImp..ScreenWidth = Mode.Width;
			g_UserConfigImp.).ScreenHeight = Mode.Height;
		}

		// Save antialias
		irr::gui::IGUICheckBox *Fullscreen = static_cast<irr::gui::IGUICheckBox *>(m_poGui->getRootGUIElement()->getElementFromId(EGUI_VIDEO_ANTIALIAS));
		g_UserConfigImp..AntiAlias = Fullscreen->isChecked();

		// Save shadows
		irr::gui::IGUICheckBox *Shadows = static_cast<irr::gui::IGUICheckBox *>(m_poGui->getRootGUIElement()->getElementFromId(EGUI_VIDEO_SHADOWS));
		g_UserConfigImp..Shadows = Shadows->isChecked();

		// Write config
		g_UserConfigImp..WriteConfig();
		*/
		m_eCurMenuState = eMenuStateInitOptions;
		break;
	case EGUI_VIDEO_CANCEL:
		PlayButtonClick();
		m_eCurMenuState = eMenuStateInitOptions;
		break;
	}
	return false;
}
//------------------------------------------------------------------------------
//! handle gui events for options menu
bool DtwStateMenu::MenuAudioGuiEvent( irr::gui::EGUI_EVENT_TYPE eGuiEventType, irr::gui::IGUIElement * poGuiElement )
{
	// single player hard/medium/easy
	bool bEaten = false;
	if( irr::gui::EGET_SCROLL_BAR_CHANGED == eGuiEventType  ) 
	{
		PlayButtonClick();
		return false;
	}

	if( irr::gui::EGET_BUTTON_CLICKED != eGuiEventType  ) 
	{
		// handle hover etc here
		return false;
	}
	switch( poGuiElement->getID() ) 
	{
	case EGUI_AUDIO_ENABLED:
		//m_eCurMenuState = eMenuStateInitVideo;
		PlayButtonClick();
		break;
	case EGUI_AUDIO_SOUND_VOLUME:
		//m_eCurMenuState = eMenuStateInitAudio;
		PlayButtonClick();
		break;
	case EGUI_AUDIO_MUSIC_VOLUME:
	//	m_eCurMenuState = eMenuStateInitMain;
		PlayButtonClick();
		break;
	case EGUI_AUDIO_SAVE:
		{
		ChangeSkin( eGuiSkinTypeMenu );
		m_eCurMenuState = eMenuStateInitOptions;
		PlayButtonClick();

		// Get settings
//		irr::gui::IGUICheckBox *AudioEnabled = static_cast<irr::gui::IGUICheckBox *>(m_poGui->getRootGUIElement()->getElementFromId(EGUI_AUDIO_ENABLED));
//		UserConfig.Instance().AudioEnabled = AudioEnabled->isChecked();

		irr::gui::IGUIScrollBar * poSoundVolume = static_cast<irr::gui::IGUIScrollBar *>(m_poGui->getRootGUIElement()->getElementFromId(EGUI_AUDIO_SOUND_VOLUME));
		g_UserConfigImp.m_f32SoundVolume = poSoundVolume->getPos() / 100.0f;

		//irr::gui::IGUIScrollBar * poMusicVolume = static_cast<irr::gui::IGUIScrollBar *>(m_poGui->getRootGUIElement()->getElementFromId(EGUI_AUDIO_MUSIC_VOLUME));
		//g_UserConfigImp.m_f32MusicVolume = poMusicVolume->getPos() / 100.0f;

		// Save
		g_UserConfigImp.WriteConfig();
		g_oSndMgr.SfxSetVolume( g_UserConfigImp.m_f32SoundVolume );

		}
		break;
	case EGUI_AUDIO_CANCEL:
		ChangeSkin( eGuiSkinTypeMenu );
		m_eCurMenuState = eMenuStateInitOptions;
		PlayButtonClick();
		break;
	}
	return false;
}
//------------------------------------------------------------------------------
//! handle gui events for tutorial
bool DtwStateMenu::MenuTutorialGuiEvent( irr::gui::EGUI_EVENT_TYPE eGuiEventType, irr::gui::IGUIElement * poGuiElement )
{
	// single player hard/medium/easy
	bool bEaten = false;
	if( irr::gui::EGET_BUTTON_CLICKED != eGuiEventType  ) 
	{
		// handle hover etc here
		return false;
	}
	switch( poGuiElement->getID() ) 
	{
	case EGUI_TUTORIAL_DONE:
		PlayButtonClick();
		m_eCurMenuState = eMenuStateInitChooseDifficulty;
		break;
	}
	return false;
}
//------------------------------------------------------------------------------
//! handle gui events for game over
bool DtwStateMenu::MenuGameOverGuiEvent( irr::gui::EGUI_EVENT_TYPE eGuiEventType, irr::gui::IGUIElement * poGuiElement )
{
	// single player hard/medium/easy
	bool bEaten = false;
	if( irr::gui::EGET_BUTTON_CLICKED != eGuiEventType  ) 
	{
		// handle hover etc here
		return false;
	}
	switch( poGuiElement->getID() ) 
	{
	case EGUI_GAME_OVER_DONE:
		PlayButtonClick();
		ChangeSkin( eGuiSkinTypeMenu ); // change skin to game mode
		m_eCurMenuState = eMenuStateInitMain;
		break;
	}
	return false;
}
//------------------------------------------------------------------------------
//! handle gui events for game over with high score
bool DtwStateMenu::MenuGameOverHighScoreGuiEvent( irr::gui::EGUI_EVENT_TYPE eGuiEventType, irr::gui::IGUIElement * poGuiElement )
{
	// single player hard/medium/easy
	bool bEaten = false;
	if( irr::gui::EGET_BUTTON_CLICKED != eGuiEventType  ) 
	{
		// handle hover etc here
		return false;
	}
	switch( poGuiElement->getID() ) 
	{
	case EGUI_GAME_OVER_HIGH_SCORE_ADD_SCORE:
		PlayButtonClick();
		m_eCurMenuState = eMenuStateInitAddHighScore;
		break;
	}
	return false;
}
//------------------------------------------------------------------------------
//! handle gui events to allow user to add high score
bool DtwStateMenu::MenuAddHighScoreGuiEvent( irr::gui::EGUI_EVENT_TYPE eGuiEventType, irr::gui::IGUIElement * poGuiElement )
{
	// single player hard/medium/easy
	bool bEaten = false;
	if( irr::gui::EGET_BUTTON_CLICKED != eGuiEventType  ) 
	{
		// handle hover etc here
		return false;
	}
	if( poGuiElement->getID() >= EGUI_KEY_BASE_ID )
	{
		if( poGuiElement->getID() > EGUI_KEY_BASE_ID + 28 )
		{
			// invalid id
			return false;
		}
		// its a key for inputing name
		irr::core::stringw strName = m_poAddHighScoreNameEditBox->getText();
		char cKeyChar = m_au8QwertyKeys[ poGuiElement->getID() - EGUI_KEY_BASE_ID ];
		if( '<' == cKeyChar )
		{
			PlayButtonClick();
			// back space
			wchar_t * pTemp = (wchar_t *)strName.c_str();
			wchar_t as16Buf[40];
			for( int i = 0; i <= strName.size(); i++ )
			{
				as16Buf[i] = pTemp[i];
			}
			as16Buf[ strName.size() - 1 ] = 0;
			m_poAddHighScoreNameEditBox->setText( as16Buf );
		}
		else
		{
			if( strName.size() >= 30 )
			{
				// too long
				return false;
			}
			PlayButtonClick();
			wchar_t as16Buf[3];
			as16Buf[0] = cKeyChar;
			as16Buf[1] = 0;
			strName = strName + as16Buf;
			m_poAddHighScoreNameEditBox->setText( strName.c_str() );
		}
		return false;

	}
	switch( poGuiElement->getID() ) 
	{
	case EGUI_ADD_SCORE_DONE:
		PlayButtonClick();
		irr::core::stringc strUserName = m_poAddHighScoreNameEditBox->getText();
		g_HighScoresImp.AddHighScore( m_poIrrBase->m_iGameLevel, strUserName.c_str(), m_poIrrBase->m_iGameScore, m_poIrrBase->m_iGameTime );
		m_eCurMenuState = eMenuStateInitHighScores;
		break;
	}
	return false;
}
//------------------------------------------------------------------------------
//! handle gui events to allow user to pick which level of high scores to view
bool DtwStateMenu::MenuHighScoreLevelGuiEvent( irr::gui::EGUI_EVENT_TYPE eGuiEventType, irr::gui::IGUIElement * poGuiElement )
{
	// single player hard/medium/easy
	bool bEaten = false;
	if( irr::gui::EGET_BUTTON_CLICKED != eGuiEventType  ) 
	{
		// handle hover etc here
		return false;
	}
	switch( poGuiElement->getID() ) 
	{
	case EGUI_PICK_HIGHSCORE_EASY:
		m_poIrrBase->m_iGameLevel = 1;
		PlayButtonClick();
		m_eCurMenuState = eMenuStateInitHighScores;
		break;
	case EGUI_PICK_HIGHSCORE_MEDIUM:
		m_poIrrBase->m_iGameLevel = 2;
		PlayButtonClick();
		m_eCurMenuState = eMenuStateInitHighScores;
		break;
	case EGUI_PICK_HIGHSCORE_HARD:
		m_poIrrBase->m_iGameLevel = 3;
		PlayButtonClick();
		m_eCurMenuState = eMenuStateInitHighScores;
		break;
	}
	return false;
}
